package com.yehuishou.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yehuishou.dao.SellItemDao;
import com.yehuishou.pojo.Brand;
import com.yehuishou.pojo.Configurations;
import com.yehuishou.pojo.ImageFile;
import com.yehuishou.pojo.Item;
import com.yehuishou.pojo.Models;
import com.yehuishou.pojo.SellItemQueryParameters;
import com.yehuishou.service.SellItemService;

@Service
public class SellItemServiceImpl implements SellItemService {
	@Resource
	private SellItemDao sellitemdao;

	@Override
	public Map<String, Object> sellitemnavigation(SellItemQueryParameters siqp, Integer pageNum, Integer pageSize) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<>();
		List<Brand> brand=null;
		if(siqp.getCid()==null){
			siqp.setCid(1);
			brand = sellitemdao.selbrand(siqp);
			siqp.setCid(null);
		}else{
			brand = sellitemdao.selbrand(siqp);
		}
		PageHelper.startPage(pageNum, pageSize);
		List<Item> item = sellitemdao.sellitem(siqp);
		for (int i = 0; i < item.size(); i++) {
			String imgs = item.get(i).getImage();
			if (imgs != null && !"".equals(imgs)) {
				String[] img = imgs.substring(1, imgs.length()).split(";");
				Integer s = Integer.parseInt(img[0]);
				ImageFile imagefile = sellitemdao.selimg(s);
				item.get(i).setImage(imagefile.getFilePath());
			}
		}
		PageInfo<Item> pageinfo = new PageInfo<Item>(item);
		if (siqp.getPid() != null) {
			Brand br = sellitemdao.selbrandone(siqp);
			siqp.setBrandname(br.getBrandname());
		}
		map.put("pageinfo", pageinfo);
		map.put("brand", brand);
		return map;
	}

	@Override
	public Map<String, Object> sellitem(SellItemQueryParameters siqp) {
		// TODO Auto-generated method stub
		List<Item> item = sellitemdao.sellitem(siqp);
		Map<String, Object> map = new HashMap<>();
		// 获取商品图片
		List<ImageFile> imgf = new ArrayList<ImageFile>();
		String imgs = item.get(0).getImage();
		if (imgs != null && !"".equals(imgs)) {
			String[] img = imgs.substring(1, imgs.length()).split(";");
			for (int i = 0; i < img.length; i++) {
				Integer s = Integer.parseInt(img[i]);
				ImageFile imagefile = sellitemdao.selimg(s);
				imgf.add(imagefile);
			}
		}
		//获取商品底部详情全部图片
		List<ImageFile> imgfparticulars = new ArrayList<ImageFile>();
		String imgsparticulars = item.get(0).getImageparticulars();
		if (imgsparticulars != null && !"".equals(imgsparticulars)) {
			String[] imgparticulars = imgsparticulars.substring(1, imgsparticulars.length()).split(";");
			for (int i = 0; i < imgparticulars.length; i++) {
				Integer s = Integer.parseInt(imgparticulars[i]);
				ImageFile imagefile = sellitemdao.selimg(s);
				imgfparticulars.add(imagefile);
			}
		}
		List<Configurations> conf = sellitemdao.configurations(siqp);
		Models model = sellitemdao.selmodel(item.get(0).getMid());
		map.put("item", item);
		map.put("imgf", imgf);
		map.put("imgfparticulars", imgfparticulars);
		map.put("conf", conf);
		map.put("model", model);
		return map;
	}

}
